package day01;

/**
 * @author wang hao
 * @date 2023-02-28 20:38
 *  二分查找法
 */
public class BinarySearch {
    public static int search(int[] arr, int target){
        return search(arr,0,arr.length - 1,target);
    }

    public static int search(int[] arr, int L, int R, int target){
        if (L > R) return -1;

        int mid = L + ((R - L) >> 1);

        if (arr[mid] == target)
            return mid;

        if (arr[mid] < target)
            return search(arr, mid + 1, R, target);

        return search(arr, L, mid - 1, target);
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};
        int search = search(arr, 10);
        System.out.println(search);
    }
}
